package com.system.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.exception.BusinessException;
import com.system.common.exception.BusinessExceptionEnum;
import com.system.common.resp.MemberLoginResp;
import com.system.common.utils.EntityConverterUtil;
import com.system.common.utils.JwtUtil;
import com.system.common.utils.SnowUtil;
import com.system.member.domain.Member;
import com.system.member.req.MemberLoginReq;
import com.system.member.req.MemberRegisterReq;
import com.system.member.req.MemberSendCodeReq;
import com.system.member.service.MemberService;
import com.system.member.mapper.MemberMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Admin
* @description 针对表【member(会员)】的数据库操作Service实现
* @createDate 2024-03-18 13:29:31
*/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member>
    implements MemberService{

    @Autowired
    private MemberMapper memberMapper;
    @Override
    public long register(MemberRegisterReq req) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",req.getMobile());
        Long l = memberMapper.selectCount(memberQueryWrapper);
        if(l==1){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        BeanUtils.copyProperties(req,member);
        long memberId = SnowUtil.getSnowflakeNextId();
        member.setId(memberId);
        memberMapper.insert(member);
        return memberId;
    }

    @Override
    public void sendCode(MemberSendCodeReq req) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",req.getMobile());
        Member memberDB = memberMapper.selectOne(memberQueryWrapper);
        String mobile = req.getMobile();

        // 如果手机号不存在，则插入一条记录
        if (ObjectUtil.isNull(memberDB)) {
            System.out.println("手机号不存在，插入一条记录");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        } else {
            System.out.println("手机号存在，不插入记录");
        }

        // 生成验证码
        // String code = RandomUtil.randomString(4);
        String code = "8888";
        System.out.println("生成短信验证码：{}"+ code);

        // 保存短信记录表：手机号，短信验证码，有效期，是否已使用，业务类型，发送时间，使用时间
        System.out.println("保存短信记录表");

        // 对接短信通道，发送短信
        System.out.println("对接短信通道");
    }

    @Override
    public MemberLoginResp login(MemberLoginReq req) {
        String mobile = req.getMobile();
        String code = req.getCode();
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",req.getMobile());
        Member memberDB = memberMapper.selectOne(memberQueryWrapper);
        // 如果手机号不存在，则插入一条记录
        if (ObjectUtil.isNull(memberDB)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        // 校验短信验证码
        if (!"8888".equals(code)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
        String token = JwtUtil.createToken(memberLoginResp.getId(), memberLoginResp.getMobile());
        memberLoginResp.setToken(token);
        return memberLoginResp;
    }
}




